package com.dc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.ResponseResultVO;
import com.dc.entity.WareHouse;
import com.dc.entity.WareHouseAddress;
import com.dc.entity.ro.WareHouseRO;
import com.dc.mapper.WareHouseAddressMapper;
import com.dc.mapper.WareHouseMapper;
import com.dc.service.WareHouseService;
import com.dc.util.RedisUtil;
import com.dc.util.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.dc.common.ResponseResultVO.checkParameterIsNotNull;

@Service
public class WareHouseServiceImpl extends ServiceImpl<WareHouseMapper, WareHouse> implements WareHouseService {

    @Resource
    private WareHouseMapper wareHouseMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private WareHouseAddressMapper addressMapper;


    @Override
    public ResponseResultVO insertWareHouse(WareHouseRO ro) {
        checkParameterIsNotNull(ro.getGoodsName(), "危险品名称不能为空");
        checkParameterIsNotNull(ro.getGrade(), "火灾危险等级不能为空");
        checkParameterIsNotNull(ro.getSpecs(), "规格不能为空");
        checkParameterIsNotNull(ro.getUnitMeasure(), "计量单位不能为空");
        WareHouse wareHouse = new WareHouse();
        BeanUtils.copyProperties(ro, wareHouse);
        wareHouse.setId(SnowflakeIdWorker.generateId());
        wareHouse.setNumber(redisUtil.serialNumber("WXP"));
        return ResponseResultVO.insertResult(wareHouseMapper.insert(wareHouse));
    }

    @Override
    public ResponseResultVO updateWareHouse(WareHouseRO ro) {
        checkParameterIsNotNull(String.valueOf(ro.getId()), "id不能为空");
        WareHouse wareHouse = new WareHouse();
        BeanUtils.copyProperties(ro, wareHouse);
        return ResponseResultVO.updateResult(wareHouseMapper.updateById(wareHouse));
    }

    @Override
    @Transactional
    public ResponseResultVO deleteWareHouse(String ids) {
        checkParameterIsNotNull(ids, "id不能为空");
        List<String> idList = Arrays.asList(ids.split(","));
        List<WareHouse> wareHouses = wareHouseMapper.selectList(new QueryWrapper<WareHouse>().lambda().in(WareHouse::getId, idList).gt(WareHouse::getStockNum, 0));
        if (CollectionUtils.isNotEmpty(wareHouses)) {
            ResponseResultVO.failResult("存在库存大于0的危险品,不允许删除！");
        }
        return ResponseResultVO.deleteResult(wareHouseMapper.deleteBatchIds(idList));
    }

    @Override
    public ResponseResultVO selectWareHouseList(String name, String regionName) {
        LambdaQueryWrapper<WareHouse> queryWrapper = new QueryWrapper<WareHouse>().lambda();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.eq(WareHouse::getGoodsName, name);
        }
        if (StringUtils.isNotBlank(regionName)) {
            List<WareHouseAddress> wareHouseAddresses = addressMapper.selectList(new QueryWrapper<WareHouseAddress>().lambda().eq(WareHouseAddress::getRegionName, regionName).gt(WareHouseAddress::getStockNum, 0));
            Set<Long> ids = wareHouseAddresses.stream().map(WareHouseAddress::getWareHouseId).collect(Collectors.toSet());
            if (CollectionUtils.isNotEmpty(ids)) {
                queryWrapper.in(WareHouse::getId, ids);
            }
        }
        return ResponseResultVO.successResult(wareHouseMapper.selectList(queryWrapper));
    }

    @Override
    public ResponseResultVO selectWareHouseListByPage(WareHouseRO ro) {
        Page page = new Page<Map<String, Object>>(ro.getCurrentPage(), ro.getPageSize());
        LambdaQueryWrapper<WareHouse> queryWrapper = new QueryWrapper<WareHouse>().lambda();
        if (StringUtils.isNotBlank(ro.getGoodsName())) {
            queryWrapper.like(WareHouse::getGoodsName, ro.getGoodsName());
        }
        if (StringUtils.isNotBlank(ro.getNumber())) {
            queryWrapper.like(WareHouse::getNumber, ro.getNumber());
        }
        queryWrapper.orderByDesc(WareHouse::getCreateTime);
        return ResponseResultVO.resultList(wareHouseMapper.selectPage(page, queryWrapper), ro);

    }
}
